/**
 * 
 */
package galacticthrone.map.render;

import java.awt.Color;

import galacticthrone.empire.Empire;
import galacticthrone.map.data.obs.SolarSystem;
import galacticthrone.screen.map.MapRenderer;
import galacticthrone.screen.text.Font;

import javax.media.opengl.GL;

// TODO Javadoc
/**
 * <br>
 *
 * @author Jaco van der Westhuizen
 */
public class SolarSystemIcon extends MapIcon
{
    private static final float COS45 = (float)Math.sqrt(.5);

    private boolean isExplored; 

    public SolarSystemIcon(SolarSystem system, boolean isExplored)
    {
    	// TODO Planet Icon when explored
        super(system, 1.0f, (isExplored) ? system.star.color.texFileName : system.star.color.texFileName);

        this.isExplored = isExplored;
    }
    
    public void setExplored(boolean isNowExplored)
    {
    	if (this.isExplored == isNowExplored)
    		return;
    	
    	this.isExplored = isNowExplored;
    	
    	// TODO Planet Icon when explored
    	if (this.isExplored)
    		super.setIcon(((SolarSystem)ob).star.color.texFileName);
    	else
    		super.setIcon(((SolarSystem)ob).star.color.texFileName);
    }

    @Override
    public void renderPicking(GL gl)
    {
    	final float posX = ob.getPosX();
    	final float posY = ob.getPosY();
    	final float ang = size * COS45;

        gl.glPushName(index);

        gl.glBegin(GL.GL_TRIANGLE_FAN);
        gl.glVertex2f(posX,        posY);
        gl.glVertex2f(posX + size, posY);
        gl.glVertex2f(posX + ang,  posY - ang);
        gl.glVertex2f(posX,        posY - size);
        gl.glVertex2f(posX - ang,  posY - ang);
        gl.glVertex2f(posX - size, posY);
        gl.glVertex2f(posX - ang,  posY + ang);
        gl.glVertex2f(posX,        posY + size);
        gl.glVertex2f(posX + ang,  posY + ang);
        gl.glVertex2f(posX + size, posY);
        gl.glEnd();

        gl.glPopName();
    }

	/* (non-Javadoc)
     * @see galacticthrone.map.render.MapIcon#renderDisplay(javax.media.opengl.GL)
     */
    @Override
    public void renderText(GL gl)
    {
	    final float posX = ob.getPosX();
	    final float posY = ob.getPosY();

	    if (isExplored)
	    {
	    	Empire owner = ((SolarSystem)ob).getOwner();
	    	
	    	MapRenderer.FONT.write(gl, ((SolarSystem)ob).name, (owner != null) ? owner.color.getFlagColor() : Color.GRAY, 0.5f, Font.ALIGN_CENTER, posX-3, posX+3, posY-size-0.5f, posY-size);
	    }
    }
}
